package com.arron.algorithm.leetcodetop100.回溯.岛屿;

public class 岛屿的最大面积 {



    int maxArea;

    int temp;

    public int maxAreaOfIsland(int[][] grid) {

        int m = grid.length;
        int n = grid[0].length;


        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {

                if (grid[i][j] == 1){
                    temp = 0;
                    dfs(grid,i,j);
                    maxArea = Math.max(temp,maxArea);
                }

            }

        }

        return maxArea;
    }

    private void dfs(int[][] grid, int i, int j) {

        if (i < 0 || j<0||i>=grid.length || j >= grid[0].length || grid[i][j] == 0){
            return;
        }

        temp++;

        grid[i][j] = 0;

        //下边
        dfs(grid,i+1,j);
        //上边
        dfs(grid,i-1,j);
        //左边
        dfs(grid,i,j-1);
        //右边
        dfs(grid,i,j+1);

    }

    public static void main(String[] args) {
        岛屿的最大面积 maxArea = new 岛屿的最大面积();

        int[][] grid = {{0,0,1,0,0,0,0,1,0,0,0,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},
                {0,1,1,0,1,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,1,0,0},
                {0,1,0,0,1,1,0,0,1,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,1,0,0},
                {0,0,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,1,1,0,0,0,0}};

        maxArea.maxAreaOfIsland(grid);

    }

}
